package com.cn.por.util.sysapi;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;

public class LinuxApi {
	
	public void test() {
		//DefaultResultSetHandler
		System.out.println("调用系统api开始");
		try {
			open("/app/soft/a.txt",true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("调用系统api结束");
	}

	private native void open(String name, boolean append)
	        throws FileNotFoundException;
    /**
    * 字符串转换UTF-8编码
    *
    * @param string 字符串
    * @return java.lang.String
    * @date 2022/4/14.
    */
    public static String toUtf8String(String string) {
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c <= 255) {
                stringBuffer.append(c);
            } else {
                byte[] b;
                try {
                    b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
                } catch (Exception ex) {
                    b = new byte[0];
                }
                for (int value : b) {
                    int k = value;
                    if (k < 0) k += 256;
                    stringBuffer.append("%").append(Integer.toHexString(k).toUpperCase());
                }
            }
        }
        return stringBuffer.toString();
    }
    
	public static void main(String[] args) {
		String e = "good";
		String b= "good";
		String d = new String("good");
		
		byte[] aa = e.getBytes();
		try {
			String st = new String(aa,"utf-8");
			System.out.println(st);
			System.out.println(e==st);
			
			System.out.println(e==b);
			
			System.out.println(e==d);
			System.out.println(e==st);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//String a = toUtf8String("good");
		//System.out.println(a);
//		LinuxApi a = new LinuxApi();
//		a.test();
	}
}
